﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="InputAction.cs" company="">
//   
// </copyright>
// <summary>
//   The input action.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

/*
 * Author: Juan Mompean Esteban
 * Created: 26-11-2011
 */
namespace ITU_vs_Zombies.Menu
{
    using System.Diagnostics.Contracts;
    using System.Linq;

    using Microsoft.Xna.Framework.Input;

    /// <summary>
    /// The input action.
    /// </summary>
    public class InputAction
    {
        #region Constants and Fields

        /// <summary>
        /// The keys.
        /// </summary>
        private readonly Keys[] keys;

        /// <summary>
        /// The new press only.
        /// </summary>
        private readonly bool newPressOnly;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="InputAction"/> class.
        /// </summary>
        /// <param name="keys">
        /// The keys.
        /// </param>
        /// <param name="newPressOnly">
        /// The new press only.
        /// </param>
        public InputAction(Keys[] keys, bool newPressOnly)
        {
            this.keys = keys != null ? keys.Clone() as Keys[] : new Keys[0];

            this.newPressOnly = newPressOnly;
        }

        #endregion

        #region Delegates

        /// <summary>
        /// The key press.
        /// </summary>
        /// <param name="key">
        /// The key.
        /// </param>
        private delegate bool KeyPress(Keys key);

        #endregion

        #region Public Methods

        /// <summary>
        /// The evaluate.
        /// </summary>
        /// <param name="state">
        /// The state.
        /// </param>
        /// <returns>
        /// The evaluate.
        /// </returns>
        public bool Evaluate(InputState state)
        {
            Contract.Requires(state != null);
            KeyPress keyTest;
            if (this.newPressOnly)
            {
                keyTest = state.IsNewKeyPress;
            }
            else
            {
                keyTest = state.IsKeyPressed;
            }

            return this.keys.Any(key => keyTest(key));
        }

        #endregion
    }
}